﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using WHW_NetUtility;

namespace MainFrame
{
    public partial class FormUserManage : Form
    {
        //DB_base GlobalVar.dbOperate = new sqliteHelper("User.db", "123");
        SQLiteDataReader sqliteReader;
        //string GlobalVar.tableName = "UserInfo";
        //string[] GlobalVar.tableCols = new string[] {"UserName","Password","Level" };
        int selectRow = 0;
        int selectRowOld = 0;
        public FormUserManage()
        {
            InitializeComponent();
        }

        private void FormUserManage_Load(object sender, EventArgs e)
        {

            //dgv_userInfo.RowHeadersWidth = 25;
            cbx_userLevel.SelectedIndex = 0;
            //bool recodeb= sqliteReader.Read();
            queryAndDisplay();
        }
        private void queryAndDisplay()
        {
            SQLiteDataReader temp = null;
            dgv_userInfo.Rows.Clear();
            GlobalVar.dbOperate.QueryFullTable(GlobalVar.tableName, ref temp);
            if (temp==null)
            {
                return;
            }
            while (temp.Read())
            {
                string UserName = temp.GetString(temp.GetOrdinal("UserName"));
                string UserPassword = temp.GetString(temp.GetOrdinal("Password"));
                string UserLevel = temp.GetString(temp.GetOrdinal("Level"));
                string[] lvContext = new string[] { UserName, UserPassword, UserLevel };
                DataGridViewOpe.Add(dgv_userInfo, lvContext);
                dgv_userInfo.Rows[dgv_userInfo.Rows.Count - 1].Selected = true;
                updateTxt(dgv_userInfo.Rows.Count - 1);

            }
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            SQLiteDataReader temp = null;
            string[] insertCon;
            insertCon = getTbxText();
            if (insertCon!=null)
            {
                GlobalVar.dbOperate.ReadTable(GlobalVar.tableName, GlobalVar.tableCols[0], "=", insertCon[0], ref temp);
                if (temp==null)
                {
                    MessageBox.Show("添加失败，请检查数据库及表");
                    return;
                }
                if (temp.Read())
                {
                    MessageBox.Show("添加时，不允许用户名重复");
                    return;
                }
                GlobalVar.dbOperate.InsertInto(GlobalVar.tableName, insertCon,ref temp);
                queryAndDisplay();
                updateTxt(dgv_userInfo.Rows.Count - 1);
            }

        }

        private string[] getTbxText()
        {
            try
            {
                string[] ret;
                string userName_s = tbx_userName.Text.Trim();
                string password_s = tbx_password.Text.Trim();
                string userLevel_s = cbx_userLevel.SelectedItem.ToString().Trim();
                if (string.IsNullOrEmpty(userName_s) || string.IsNullOrEmpty(password_s) || string.IsNullOrEmpty(userLevel_s))
                {
                    MessageBox.Show("用户名、用户密码、用户权限均不能为空");
                    return null;
                }
                else
                {
                    ret = new string[] { userName_s, password_s, userLevel_s };
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            
        }
        private void btn_closeForm_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            //queryAndDisplay();

            SQLiteDataReader temp = null;
            string[] opera = new string[] { "=", "=", "=" };
            string[] colOpera = new string[] { "AND", "AND" };
            string[] readCon= getTbxText();
            GlobalVar.dbOperate.ReadTable(GlobalVar.tableName, null, GlobalVar.tableCols, opera, colOpera, readCon, ref temp);
            dgv_userInfo.Rows.Clear();
            if (temp==null)
            {
                MessageBox.Show("查询失败，请检查数据及表信息");
                return;
            }
            while (temp.Read())
            {
                string UserName = temp.GetString(temp.GetOrdinal("UserName"));
                string UserPassword = temp.GetString(temp.GetOrdinal("Password"));
                string UserLevel = temp.GetString(temp.GetOrdinal("Level"));
                string[] lvContext = new string[] { UserName, UserPassword, UserLevel };
                DataGridViewOpe.Add(dgv_userInfo, lvContext);
                dgv_userInfo.Rows[dgv_userInfo.Rows.Count - 1].Selected = true;
                updateTxt(dgv_userInfo.Rows.Count - 1);

            }
        }

        private void dgv_userInfo_SelectionChanged(object sender, EventArgs e)
        {
            selectRow = dgv_userInfo.CurrentRow.Index;
            updateTxt(selectRow);
        }
        private void updateTxt(int selectIndex)
        {
            try
            {
                if (dgv_userInfo.Rows != null&&dgv_userInfo.Rows[selectIndex].Cells[1].Value!=null)
                {
                    tbx_userName.Text = dgv_userInfo.Rows[selectIndex].Cells[1].Value.ToString();
                    tbx_password.Text = dgv_userInfo.Rows[selectIndex].Cells[2].Value.ToString();
                    string level = dgv_userInfo.Rows[selectIndex].Cells[3].Value.ToString();
                    if (level == "操作员")
                    {
                        cbx_userLevel.SelectedIndex = 0;
                    }
                    else if (level == "技术员")
                    {
                        cbx_userLevel.SelectedIndex = 1;
                    }
                    else if (level == "管理员")
                    {
                        cbx_userLevel.SelectedIndex = 2;
                    }
                    else
                    {
                        MessageBox.Show("权限只能是操作员、技术员、管理员，请检查！");
                    }
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btn_update_Click(object sender, EventArgs e)
        {
            SQLiteDataReader temp = null;
            string[] updateCon;
            selectRowOld = dgv_userInfo.SelectedRows[0].Index;
            string oldName = dgv_userInfo.SelectedRows[0].Cells[1].Value.ToString();
            updateCon = getTbxText();
            if (updateCon==null)
            {
                return;
            }
            GlobalVar.dbOperate.ReadTable(GlobalVar.tableName, GlobalVar.tableCols[0], "=", updateCon[0],ref temp);
            GlobalVar.dbOperate.UpdateInfo(GlobalVar.tableName, GlobalVar.tableCols, updateCon, GlobalVar.tableCols[0], oldName, " = ");
            queryAndDisplay();
            dgv_userInfo.Rows[dgv_userInfo.Rows.Count - 1].Selected = false;
            dgv_userInfo.Rows[selectRowOld].Selected = true;
            updateTxt(selectRowOld);
            //if (!temp.Read())
            //{
            //    GlobalVar.dbOperate.UpdateInfo(GlobalVar.tableName, GlobalVar.tableCols, colValue, GlobalVar.tableCols[0], oldName, " = ");
            //    queryAndDisplay();
            //}
            //else
            //{
            //    MessageBox.Show("用户名不允许重复");
            //}
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            SQLiteDataReader temp = null;
            int selectold = dgv_userInfo.SelectedRows[0].Index;
            string[] deleteCon;
            string[] opera = new string[] { "=", "=", "=" };
            string[] colOpera = new string[] { "AND","AND"};
            selectRowOld = dgv_userInfo.SelectedRows[0].Index;
            string oldName = dgv_userInfo.SelectedRows[0].Cells[1].Value.ToString();
            deleteCon = getTbxText();
            if (deleteCon == null)
            {
                return;
            }
            GlobalVar.dbOperate.ReadTable(GlobalVar.tableName, GlobalVar.tableCols[0], "=", deleteCon[0], ref temp);
            GlobalVar.dbOperate.DeleteInfo(GlobalVar.tableName, GlobalVar.tableCols,opera, colOpera, deleteCon);
            queryAndDisplay();
            if (dgv_userInfo.Rows.Count>2)
            {
                dgv_userInfo.Rows[dgv_userInfo.Rows.Count - 1].Selected = false;
                dgv_userInfo.Rows[selectold - 1].Selected = true;
            }

            updateTxt(selectold-1);
        }

        private void btn_queryAll_Click(object sender, EventArgs e)
        {
            queryAndDisplay();
        }
        private void closeForm()
        {
            try
            {
                GlobalVar.dbOperate.CloseDB();
                //Application.Exit();
                //this.Close();
            }
            catch (System.Exception ex)
            {
                Log4Helper.WriteLog("FormUserManage.closeForm", ex);
                MessageBox.Show("退出窗口异常");
            }

        }

        private void FormUserManage_FormClosed(object sender, FormClosedEventArgs e)
        {
            closeForm();
        }

        private void btn_createTable_Click(object sender, EventArgs e)
        {
            GlobalVar.dbOperate.CreateTable(GlobalVar.tableName,GlobalVar.tableCols,GlobalVar.tableTypes);
        }
    }
}
